package org.hisp.dhis.wsclient.dao.mockup;

import java.util.Vector;

import org.hisp.dhis.wsclient.dao.OrganisationUnitDAO;
import org.hisp.dhis.wsclient.model.OrganisationUnit;


/**
 * @author Philip Bruvoll
 * @version $Id: MockupOrganisationUnitDAO.java 28 2008-09-10 12:36:31Z philipjb
 *          $
 */
public class MockupOrganisationUnitDAO
    implements OrganisationUnitDAO
{

    Vector levels;

    public MockupOrganisationUnitDAO()
    {
        Vector level;
        levels = new Vector();

        level = new Vector();
        levels.addElement( level );
        for ( int i = 0; i < 10; i++ )
        {
            addOrgunit( i, level );
        }

        level = new Vector();
        levels.addElement( level );
        for ( int i = 0; i < 5; i++ )
        {
            addOrgunit( i + 100, level );
        }

        level = new Vector();
        levels.addElement( level );
        for ( int i = 0; i < 10; i++ )
        {
            addOrgunit( i + 200, level );
        }

        level = new Vector();
        levels.addElement( level );
        for ( int i = 0; i < 7; i++ )
        {
            addOrgunit( i + 300, level );
        }

    }

    private void addOrgunit( int id, Vector vec )
    {
        vec.addElement( makeOrgunit( id, "Orgunit " + id + " (L:" + id / 100 + ")" ) );
    }

    private OrganisationUnit makeOrgunit( int id, String name )
    {
        OrganisationUnit orgunit = new OrganisationUnit();
        orgunit.setId( id );
        orgunit.setName( name );
        return orgunit;
    }

    public OrganisationUnit getOrganisationUnit( int orgUnitId )
    {
        return (OrganisationUnit) ((Vector) levels.elementAt( 0 )).elementAt( 0 );
    }

    public Vector listOrganisationUnits( OrganisationUnit parent )
    {
        if ( parent == null )
        {
            return (Vector) levels.elementAt( 0 );
        }
        else
        {
            int id = parent.getId();
            try
            {
                return (Vector) levels.elementAt( (id / 100) + 1 );
            }
            catch ( Exception e )
            {
                return new Vector();
            }
        }
    }
}
